package com.blogspot.nhu313.myhistory.category;

import java.util.ArrayList;
import java.util.List;

import android.provider.BaseColumns;

import com.blogspot.nhu313.myhistory.service.db.DBField;
import com.blogspot.nhu313.myhistory.service.db.SQLStringBuilder;

public class CategorySQLStringBuilder implements BaseColumns{
	
	protected static final String TABLE_NAME = "Category";
	
	public static String createTable(){
		List<DBField> columns = new ArrayList<DBField>();
		columns.add(new DBField(CategoryFieldName._id.name(), DBField.Type.INT_PRIMARY_KEY));
		columns.add(new DBField(CategoryFieldName.name.name(), DBField.Type.VARCHAR_NOT_NULL));
		columns.add(new DBField(CategoryFieldName.parent_id.name(), DBField.Type.INT));
		return SQLStringBuilder.createTable(TABLE_NAME, columns);
	}
	
	public static String deleteTable(){
		return SQLStringBuilder.deleteTable(TABLE_NAME);
	}
	
	public static String getCategories(int parentCategoryId){
		String columns = createColumns();
		String whereCondition = CategoryFieldName.parent_id.name() + "=" + parentCategoryId;
		return SQLStringBuilder.queryWithCondition(TABLE_NAME, columns, whereCondition);
	}

	private static String createColumns() {
		StringBuilder builder = new StringBuilder(16);
		for (CategoryFieldName field : CategoryFieldName.values()){
			if (field.ordinal() != 0){
				builder.append(SQLStringBuilder.SEPARATOR);
			}

			builder.append(field);
		}
		return builder.toString();
	}	
}
